package com.fitso.model.dao.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.util.Assert;

import com.fitso.model.bean.nutrition.Food;
import com.fitso.model.dao.FoodDao;

/**
 * Validating aspect for different {@link FoodDao}s. This is the first check to
 * prevent data manipulations from happening and/or throwing their own
 * exceptions. This is to check data integrity not business rules. So while
 * calories = -100 is an invalid it is perfectly valid for the data store
 * 
 * @author timothystorm
 */
@Aspect
public class FoodDataIntegrityAspect {
	@Before("execution(* com.fitso.model.dao.*.saveOrUpdate(..)) && args(food)")
	public void validateSaveOrUpdate(Food food) {
		Assert.notNull(food, "food is a required parameter");
		Assert.notNull(food.getCalories(), "calorie is a required attribute");
	}

	@Before("execution(* com.fitso.model.dao.*.delete(..)) && args(food)")
	public void validateDelete(Food food) {
		Assert.notNull(food, "food is a required parameter");
		Assert.state(food.getId() != null && food.getId() >= 0, "food id not valid or in a persistent state");
	}
}
